package patterndesign.chainofresponsibility;

import java.util.ArrayList;
import java.util.List;

public class ChainOfResponsibility {
    public static void main(String[] args) {
        Msg msg = new Msg();
        msg.setMsg("大家好:)，<script>，欢迎访问 mashibing.com，大家都是996");


        FilterChain chain1 = new FilterChain().add(new SensitiveFilter()).add(new HTMLFilter());
        FilterChain chain2 = new FilterChain().add(new FaceFilter()).add(new URLFilter());
        // 合并chain
        chain1.add(chain2);
        chain1.doFilter(msg); // chain1.handleFilter(msg);
        System.out.println(msg);

    }
}

interface Filter {
    void doFilter(Msg msg);
}

/**
 * 处理HTML里面的关键字
 */
class HTMLFilter implements Filter {
    @Override
    public void doFilter(Msg msg) {
        String r = msg.getMsg();
        r = r.replace('<', '[');
        r = r.replace('>', ']');
        msg.setMsg(r);
    }

    @Override
    public String toString() {
        return "HTMLFilter";
    }
}

/**
 * 处理敏感字
 */
class SensitiveFilter implements Filter {
    @Override
    public void doFilter(Msg msg) {
        String r = msg.getMsg();
        r = r.replace("996", "955");
        msg.setMsg(r);
    }

    @Override
    public String toString() {
        return "SensitiveFilter";
    }
}

/**
 * 处理表情
 */
class FaceFilter implements Filter {
    @Override
    public void doFilter(Msg msg) {
        String r = msg.getMsg();
        r = r.replace(":)", "^_^");
        msg.setMsg(r);
    }

    @Override
    public String toString() {
        return "FaceFilter";
    }
}

/**
 * 处理表情
 */
class URLFilter implements Filter {
    @Override
    public void doFilter(Msg msg) {
        String r = msg.getMsg();
        r = r.replace("mashibing.com", "cnblogs.com");
        msg.setMsg(r);
    }

    @Override
    public String toString() {
        return "URLFilter";
    }
}

/**
 * todo 这个操作有点儿让人费解  分开这两个操作或许好点
 */
class FilterChain implements Filter {
    List<Filter> filters = new ArrayList<>();

    public FilterChain add(Filter filter) {
        filters.add(filter);
        return this;
    }

    /**
     * 实际上该方法有两个职责
     * 1) 作为单个filter的职责
     * 2) 作为chain的职责
     * <p>
     * 妙啊。
     *
     * @param msg
     */
    @Override
    public void doFilter(Msg msg) {
        // 一个一个处理
        for (Filter f : filters) {
            System.out.println("doFilter method filter:" + f);
            f.doFilter(msg);
        }
    }

    /**
     * 将上面函数的两个职责分开
     *
     * @param msg 待处理的消息
     */
    public void handleFilter(Msg msg) {
        for (Filter f : filters) {
            System.out.println("doFilter method filter:" + f);
            f.doFilter(msg);
        }
    }


    @Override
    public String toString() {
        return "FilterChain{" +
                "filters=" + filters +
                '}';
    }
}

class Msg {
    String name;
    String msg;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    @Override
    public String toString() {
        return "Msg{" +
                "msg='" + msg + '\'' +
                '}';
    }
}